package com.my.springframework.aop.aspectj;

import com.my.springframework.aop.BeforeAdvice;
import com.my.springframework.aop.Pointcut;
import com.my.springframework.aop.PointcutAdvisor;
import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;

/**
 * Spring AOP Advisor that can be used for any AspectJ pointcut expression.
 *
 * @author sk
 * @description
 * @since 2021-11-30
 **/
public class AspectJExpressionPointcutAdvisor implements PointcutAdvisor {

    // 切面
    private AspectJExpressionPointcut pointcut;
    // 具体的拦截方法
    private Advice advice;
    // 表达式
    private String expression;

    public void setAdvice(Advice advice) {
        this.advice = advice;
    }

    public void setExpression(String expression) {
        this.expression = expression;
    }

    /**
     * Return the advice part of this aspect. An advice may be an
     * interceptor, a before advice, a throws advice, etc.
     *
     * @return the advice that should apply if the pointcut matches
     * @see MethodInterceptor
     * @see BeforeAdvice
     */
    @Override
    public Advice getAdvice() {
        return advice;
    }

    /**
     * Get the Pointcut that drives this advisor.
     */
    @Override
    public Pointcut getPointcut() {
        if (null == pointcut) {
            pointcut = new AspectJExpressionPointcut(expression);
        }
        return pointcut;
    }
}
